//
//---------------------  1D  ---------------------
//
//
// CentralDiff is not defined for 1D
//

//
//---------------------  2D  ---------------------
//
//       Staggered Mesh for u-vel and v-vel
//
//   0       1       2       3       4       5   
//
//5      >       >       >       >       > 
//       |       |       |       |       |              
//   ^---+---^---+---^---+---^---+---^---+---^  4       Mesh for scalar fields
//       |       |       |       |       | 
//4      >   o   >   o   >   o   >   o   >               5  x-x-+-x-+-x-+-x-x
//       |       |       |       |       |               4  x o | o | o | o x
//   ^---+---^---+---^---+---^---+---^---+---^  3           +---+---+---+---+
//       |       |       |       |       |               3  x o | o | o | o x
//3      >   o   >   o   >   o   >   o   >                  +---+---+---+---+
//       |       |       |       |       |               2  x o | o | o | o x
//   ^---+---^---+---^---+---^---+---^---+---^  2           +---+---+---+---+
//       |       |       |       |       |               1  x o | o | o | o x
//2      >   o   >   o   >   o   >   o   >               0  x-x-+-x-+-x-+-x-x
//       |       |       |       |       |                  
//   ^---+---^---+---^---+---^---+---^---+---^  1           0 1   2   3   4 5 
//       |       |       |       |       |                 
//1      >   o   >   o   >   o   >   o   >                 o central node
//       |       |       |       |       |                 x boundary node
//   ^---+---^---+---^---+---^---+---^---+---^  0          > u velocity 
//       |       |       |       |       |                 ^ v velocity
//0      >       >       >       >       >    
//       0       1       2       3       4             
//
//                                                  
//          Volumes for u-velocity
//
//   0       1       2       3       4       5
//
//5      >       >       >       >       > 
//       :       :       :       :       :              
//   ^...+---^---+---^---+---^---+---^---+...^  4       Mesh for scalar fields
//       |   |   :   |   :   |   :   |   | 
//4      >   o   >   o   >   o   >   o   >               5  x-x-+-x-+-x-+-x-x
//       |   |   :   |   :   |   :   |   |               4  x o | o | o | o x
//   ^...+---^---+---^---+---^---+---^---+...^  3           +---+---+---+---+
//       |   |   :   |   :   |   :   |   |               3  x o | o | o | o x
//3      >   o   >   o   >   o   >   o   >                  +---+---+---+---+
//       |   |   :   |   :   |   :   |   |               2  x o | o | o | o x
//   ^...+---^---+---^---+---^---+---^---+...^  2           +---+---+---+---+
//       |   |   :   |   :   |   :   |   |               1  x o | o | o | o x
//2      >   o   >   o   >   o   >   o   >               0  x-x-+-x-+-x-+-x-x
//       |   |   :   |   :   |   :   |   |                  
//   ^...+---^---+---^---+---^---+---^---+...^  1           0 1   2   3   4 5 
//       |   |   :   |   :   |   :   |   |                 
//1      >   o   >   o   >   o   >   o   >                 o central node
//       |   |   :   |   :   |   :   |   |                 x boundary node
//   ^...+---^---+---^---+---^---+---^---+...^  0          > u velocity 
//       :       :       :       :       :                 ^ v velocity
//0      >       >       >       >       >    
//
//       0       1       2       3       4              
//
//                       
//                  |           |           |           |
//                --^-----------^-----------^-----------^-- 
//                  |     :     |     :     |     :     |               
//                  |     :     |  (i,j+1)  |     :     |
//                  o     >     o    u_N    o     >     o   
//                  |     :     |     :     |     :     |
//                  |     :     |     :     |     :     |
//                --^---------- 3 -- v_n -- 4 ----------^--  4 = v(i+1, j  )
//                  |     :     |     :     |     :     |    3 = v(i  , j  )
//                  |     :     |     :     |     :     |    2 = v(i+1, j-1)
//                  o    u_W   u_w   u_P   u_e   u_E    o    1 = v(i  , j-1)
//                  |  (i-1,j)  |   (i,j)   |  (i+1,j)  |
//                  |     :     |     :     |     :     |
//                --^---------- 1 -- v_s -- 2 ----------^-- 
//                  |     :     |     :     |     :     |               
//                  |     :     |     :     |     :     |
//                  o     >     o    u_S    o     >     o   
//                  |     :     |  (i,j-1)  |     :     |
//                  |     :     |     :     |     :     |
//                --^-----------^-----------^-----------^--
//                  |           |           |           | 
//                   
//   u_w = ( u(i-1,j) + u(i,j) ) / 2     u_e = ( u(i+1,j) + u(i,j) ) / 2
//   v_n = ( v(i,j) + v(i+1,j) ) / 2     v_s = ( v(i,j-1) + v(i+1,j-1) ) / 2
//              3        4                            1          2          
//
namespace Tuna {

template<typename Tprec, int Dim>
inline bool CDS_XHay<Tprec, Dim>::calcCoefficients2D() 
{
    prec_t dy_dx = Gamma * dy / dx;
    prec_t dx_dy = Gamma * dx / dy;
    prec_t dxy_dt = dx * dy / dt;
    prec_t ce, cep, cem, cw, cwp, cwm, CE, CW;
    prec_t cn, cnp, cnm, cs, csp, csm, CN, CS;
    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aP = 0.0; 
    sp = 0.0;

    for (int i =  bi; i <= ei; ++i)
      for (int j = bj; j <= ej; ++j)
	{
	  CE = ce = ( u(i, j  ) + u(i+1,j  ) ) * 0.5 * dy;
	  CW = cw = ( u(i, j  ) + u(i-1,j  ) ) * 0.5 * dy;
	  CN = cn = ( v(i, j  ) + v(i+1,j  ) ) * 0.5 * dx;
	  CS = cs = ( v(i, j-1) + v(i+1,j-1) ) * 0.5 * dx;
	  
	  cem = cep = 0;
	  cwm = cwp = 0;
	  cnm = cnp = 0;
	  csm = csp = 0;

	  if ( ce > 0 ){
	    CE = 0;
	    cep = ce * 0.5 * (-phi_0(i,j) + phi_0(i+1,j));
	  } else {
	    cem = ce * 0.5 * (phi_0(i,j) - phi_0(i+1,j));
	  } 
	  
	  if ( cw > 0 ){
	    cwp = cw * 0.5 * (-phi_0(i-1,j) + phi_0(i,j));
	  } else {
	    CW = 0.0;
	    cwm = cw * 0.5 * (phi_0(i-1,j) - phi_0(i,j));
	  } 

	  if ( cn > 0 ){
	    CN = 0;
	    cnp = cn * 0.5 * (-phi_0(i,j) + phi_0(i,j+1));
	  } else {
	    cnm = cn * 0.5 * (phi_0(i,j) - phi_0(i,j+1));
	  } 
	  
	  if ( cs > 0 ){
	    csp = cs * 0.5 * (-phi_0(i,j-1) + phi_0(i,j));
	  } else {
	    CS = 0.0;
	    csm = cs * 0.5 * (phi_0(i,j-1) - phi_0(i,j));
	  } 

	  aE (i,j) = dy_dx - CE;
	  aW (i,j) = dy_dx + CW;
	  aN (i,j) = dx_dy - CN;
	  aS (i,j) = dx_dy + CS;
	  aP (i,j) = aE (i,j) + aW (i,j) + aN (i,j) + aS (i,j) + dxy_dt
	    + (ce - cw) + (cn - cs);
	  sp (i,j) = phi_0(i,j) * dxy_dt - ( p(i+1,j) - p(i,j) ) * dy
	    - (cep + cem - cwp - cwm + cnp + cnm - csp - csm);	    	    
	}    
    calc_du_2D();
    applyBoundaryConditions2D();
    return 0;  
}

//
//---------------------  3D  ---------------------
//
template<typename Tprec, int Dim>
inline bool CDS_XHay<Tprec, Dim>::calcCoefficients3D() 
{
    prec_t dyz = dy * dz, dyz_dx = Gamma * dyz / dx;
    prec_t dxz = dx * dz, dxz_dy = Gamma * dxz / dy;
    prec_t dxy = dx * dy, dxy_dz = Gamma * dxy / dz;
    prec_t dxyz_dt = dx * dy * dz / dt;
    prec_t ce, cep, cem, cw, cwp, cwm, CE, CW;
    prec_t cn, cnp, cnm, cs, csp, csm, CN, CS;
    prec_t cf, cfp, cfm, cb, cbp, cbm, CF, CB;
    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aF = 0.0; aB = 0.0; aP = 0.0; 
    sp = 0.0;

    for (int k = bk; k <= ek; ++k)
      for (int i =  bi; i <= ei; ++i)
	for (int j = bj; j <= ej; ++j)
	  {
	    CE = ce = ( u(i+1, j  ,k  ) + u(i  ,j  ,k  ) ) * 0.5 * dyz;
	    CW = cw = ( u(i-1, j  ,k  ) + u(i  ,j  ,k  ) ) * 0.5 * dyz;
	    CN = cn = ( v(i  , j  ,k  ) + v(i+1,j  ,k  ) ) * 0.5 * dxz;
	    CS = cs = ( v(i  , j-1,k  ) + v(i+1,j-1,k  ) ) * 0.5 * dxz;
	    CF = cf = ( w(i  , j  ,k  ) + w(i+1,j  ,k  ) ) * 0.5 * dxy;
	    CB = cb = ( w(i  , j  ,k-1) + w(i+1,j  ,k-1) ) * 0.5 * dxy;
	    cem = cep = 0;
	    cwm = cwp = 0;
	    cnm = cnp = 0;
	    csm = csp = 0;
	    cfm = cfp = 0;
	    cbm = cbp = 0;	

	    if ( ce > 0 ){
	      CE = 0;
	      cep = ce * 0.5 * (-phi_0(i,j,k) + phi_0(i+1,j,k));
	    } else {
	      cem = ce * 0.5 * (phi_0(i,j,k) - phi_0(i+1,j,k));
	    } 
	  
	    if ( cw > 0 ){
	      cwp = cw * 0.5 * (-phi_0(i-1,j,k) + phi_0(i,j,k));
	    } else {
	      CW = 0.0;
	      cwm = cw * 0.5 * (phi_0(i-1,j,k) - phi_0(i,j,k));
	    } 	    
	    
	    if ( cn > 0 ){
	      CN = 0;
	      cnp = cn * 0.5 * (-phi_0(i,j,k) + phi_0(i,j+1,k));
	    } else {
	      cnm = cn * 0.5 * (phi_0(i,j,k) - phi_0(i,j+1,k));
	    } 
	    
	    if ( cs > 0 ){
	      csp = cs * 0.5 * (-phi_0(i,j-1,k) + phi_0(i,j,k));
	    } else {
	      CS = 0.0;
	      csm = cs * 0.5 * (phi_0(i,j-1,k) - phi_0(i,j,k));
	    } 

	    if ( cf > 0 ){
	      CF = 0;
	      cfp = cf * 0.5 * (-phi_0(i,j,k) + phi_0(i,j,k+1));
	    } else {
	      cfm = cf * 0.5 * (phi_0(i,j,k) - phi_0(i,j,k+1));
	    } 
	    
	    if ( cb > 0 ){
	      cbp = cb * 0.5 * (-phi_0(i,j,k-1) + phi_0(i,j,k));
	    } else {
	      CB = 0.0;
	      cbm = cb * 0.5 * (phi_0(i,j,k-1) - phi_0(i,j,k));
	    } 

	    aE (i,j,k) = dyz_dx - CE;
	    aW (i,j,k) = dyz_dx + CW;
	    aN (i,j,k) = dxz_dy - CN;
	    aS (i,j,k) = dxz_dy + CS;
	    aF (i,j,k) = dxy_dz - CF;
	    aB (i,j,k) = dxy_dz + CB;
	    aP (i,j,k) = aE (i,j,k) + aW (i,j,k) + aN (i,j,k) + aS (i,j,k)
	      + aF (i,j,k) + aB (i,j,k) + dxyz_dt
	      + (ce - cw) + (cn - cs) + (cn - cs);
	    sp(i,j,k) += u(i,j,k) * dxyz_dt - 
	      ( p(i+1,j,k) - p(i,j,k) ) * dyz
	      - (cep + cem - cwp - cwm + cnp + cnm - csp - csm + cfp + cfm - cbp - cbm); 
	  }
    calc_du_3D();
    applyBoundaryConditions3D();   
    return 0;
}

} // Tuna namespace















